package com.cwj.mansysback.dao.impl;

import com.cwj.mansysback.dao.BaseDao;
import com.cwj.mansysback.dao.api.AccountDao;
import com.cwj.mansysback.dao.util.MD5Util;
import com.cwj.mansysback.entity.Account;

public class AccountDaoImpl extends BaseDao<Account> implements AccountDao {
    @Override
    public boolean isExists(String username,Integer rank) {
        String sql="select * from account where aid = ? and accrank = ? ";
        return super.select(Account.class,sql,username,rank) != null;
    }

    @Override
    public boolean isTrue(String username, String password, Integer rank) {
        return this.isTrue_encode(username,MD5Util.encode(password),rank);
    }

    @Override
    public boolean isTrue_encode(String username, String password,Integer rank) {
        String sql="select * from account where aid = ? and accrank = ? and pawd = ?";
        return super.select(Account.class,sql,username,rank,password) != null;
    }

    @Override
    public boolean setPassword(String name, Integer rank, String password) {
        return setPassword_encode(name,rank,MD5Util.encode(password));
    }


    @Override
    public boolean setPassword_encode(String name,Integer rank, String password) {
        String sql = "update account set pawd = ? where aid = ? and accrank = ?";
        return super.update(sql, password, name,rank) > 0;
    }

//    @Override
//    public boolean setAccRank(String name, Integer rank) {
//        if()
//        String sql = "update account set accrank = ? where aid = ?";
//        return super.update(sql, rank, name) > 0;
//    }

    @Override
    public boolean addAccount(String name, String password, Integer rank) {
        return addAccount_encode(name,MD5Util.encode(password),rank);
    }

    @Override
    public boolean addAccount_encode(String name, String password, Integer rank) {

        if(isExists(name,rank)) return false;

        String sql = "insert into account (aid,pawd,accrank) values (?,?,?)";
        return super.update(sql, name, password, rank) > 0;
    }

    @Override
    public boolean deleteAccount(String name, Integer rank) {
        String sql = "delete from account where aid = ? and accrank = ?";
        return super.update(sql,name,rank)>0;
    }

    @Override
    public Account getAccount(String name, Integer rank) {
//        最后都全部写出来，避免其以后添加列时，旧代码报错
        String sql="select aid,pawd,accrank from account where aid = ? and accrank = ?";
        return super.select(Account.class,sql,name,rank);
    }

}
